package edu.niu.mcaslab;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;

import java.util.List;

/**
 * Created by al_ho on 2013/6/28.
 * for store application's permissions
 */
public class AppPermissionDatabase {
    private Context context;
    private ProgressDialog progressDialog;
    public SQLiteDatabase database;

    public AppPermissionDatabase(Context context) {
        this.context = context;
        database = new DatabaseOpenHelper(this.context).getWritableDatabase();
    }

    public void updateDatabase(Activity activity) {
        progressDialog = new ProgressDialog(context);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progressDialog.setMessage(context.getString(R.string.dialog_update_text));
        progressDialog.setCancelable(false);

        PackageManager pm = context.getPackageManager();
        List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);
        progressDialog.setMax(packages.size());

        progressDialog.show();

        new DbUpdateTask(activity, database, progressDialog).execute();
    }

    public void isUpToDate() {
        progressDialog = new ProgressDialog(context);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progressDialog.setMessage(context.getString(R.string.dialog_uptodate_text));
        progressDialog.setCancelable(false);

        PackageManager pm = context.getPackageManager();
        List<PackageInfo> packages = pm.getInstalledPackages(PackageManager.GET_META_DATA);
        progressDialog.setMax(packages.size());

        progressDialog.show();

        new IsUpToDateTask().execute();
    }

    private class IsUpToDateTask extends AsyncTask<Void, Void, Boolean> {
        protected Boolean doInBackground(Void... params) {
            PackageManager pm = context.getPackageManager();
            List<PackageInfo> packages = pm.getInstalledPackages(PackageManager.GET_META_DATA);

            String packageName;
            String packageVersionCode;

            boolean isUpToDate = true;
            Cursor packageChange;

            packageChange = database.rawQuery("SELECT Count(*) FROM application;", null);
            packageChange.moveToFirst();

            if (packageChange.getInt(0) != packages.size() || packageChange.getInt(0) == 0) {
                packageChange.close();
                return false;
            }

            for (PackageInfo pi : packages) {
                progressDialog.incrementProgressBy(1);

                packageName = pi.packageName;
                packageVersionCode = Integer.toString(pi.versionCode);

                packageChange = database.query("application", new String[]{"id"}, "name = ? AND version_code = ?", new String[]{packageName, packageVersionCode}, null, null, null);
                if (packageChange.getCount() == 0) {
                    isUpToDate = false;
                    packageChange.close();
                    break;
                }
                packageChange.close();
            }

            return isUpToDate;
        }

        protected void onPostExecute(Boolean result) {
            progressDialog.dismiss();
            ((MainActivity) context).isUpToDateResult(result);
        }
    }
}
